﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BitcoinCommon;
using BitcoinMarket;
using BitcoinMarket.Data;
using BitcoinExchange;

using BitcoinTrading.Data;
using BitcoinTrading.Trading;

namespace BitcoinTrading
{
    public class TradingMain
    {
        public MainInfo _info;
        public MarketPrice _market;
        public Exchange _exchange;

        DataController _dataController;
        ActiveTrader _ActiveTrader;
        PassiveTrader _PassiveTrader;

        public TradingMain()
        {
            _info = new MainInfo();
            _market = new MarketPrice(_info);
            _exchange = new Exchange(_info);

            _dataController = new DataController(_market, _exchange);
            _ActiveTrader = new ActiveTrader(_info, _exchange, _dataController);
            _PassiveTrader = new PassiveTrader(_info, _exchange, _dataController, _market);
        }

        public void Login()
        {
            _info.Login();
        }

        public bool IsLogin()
        {
            return _info.IsLogin();
        }

        public DateTime RefreshTime()
        {
            return _info.TokenTime();
        }

        public double CurrentRate()
        {
            return Double.Parse(_market.CurrentRate());
        }

        public void RetrievedData()
        {
            if (!IsLogin())
                return;

            _info.RefreshToken();

            _market.RetrivedMarketPrice();

            _exchange.RetrievedOpenOrder();

            _exchange.RetrievedWallet();

            CalculateData();
        }

        public void CalculateData()
        {
            _dataController.SetMarketData();

            _dataController.CalculateAccmulate();

            _dataController.CalculateMarginalTrade();

        }

        public DataController Data()
        {
            return _dataController;
        }

        public void ExecutionTrading()
        {
        }


        public double BalanceKorbitKRW()
        {
            return _exchange.KRWAmount();
        }

        public double BalanceKorbitBTC()
        {
            return _exchange.KorbitBTCAmount();
        }

        public double BalanceBTCeUSD()
        {
            return _exchange.USDAmount();
        }

        public double BalanceBTCeBTC()
        {
            return _exchange.BTCeBTCAmount();
        }

        public void ActiveTrade()
        {
            _ActiveTrader.ExecuteTrade();
        }

        public void ActiveTradeSetMarginalPrice(double dbMarginalBuy, double dbMarginalSell)
        {
            _ActiveTrader.SetMarginalPrice(dbMarginalBuy, dbMarginalSell);
        }

        public bool ActiveTradeOnceMarginalBuy()
        {
            return _ActiveTrader.TradeMarginalBuy();
        }

        public bool ActiveTradeOnceMarginalSell()
        {
            return _ActiveTrader.TradeMarginalSell();
        }

        public void PassiveTradeSetMarginalPrice(double dbMarginalBuy, double dbMarginalSell)
        {
            
        }

        public void PassiveTrade()
        {
            _PassiveTrader.ExecuteTrade();
        }

        public void PassiveClearTrade()
        {
            _PassiveTrader.ClearOrder();
        }


    }
}
